
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.operation
{
import org.waveprotocol.wave.model.document.operation.DocumentOperation;
import org.waveprotocol.wave.model.operation.wave.AddParticipant;
import org.waveprotocol.wave.model.operation.wave.NoOp;
import org.waveprotocol.wave.model.operation.wave.RemoveParticipant;
import org.waveprotocol.wave.model.operation.wave.WaveletDocumentOperation;
import org.waveprotocol.wave.model.operation.wave.WaveletOperation;
import org.waveprotocol.wave.model.util.Preconditions;

public class AbstractOpEquator implements OpEquator
{
	public function AbstractOpEquator()
	{
	}
	
	public function equalWavelet(a:WaveletOperation, b:WaveletOperation):Boolean {
		Preconditions.checkNotNull(a, "First argument is null");
		Preconditions.checkNotNull(b, "Second argument is null");
		return equalNullableWavelet(a, b);
	}
	
	
	public function equalNullableWavelet(a:WaveletOperation, b:WaveletOperation):Boolean {
		if (a == null) {
			return b == null;
		} else if (a instanceof AddParticipant) {
			return (b instanceof AddParticipant)
			&& (AddParticipant(a)).getParticipantId().equals(
				(AddParticipant(b)).getParticipantId());
		} else if (a instanceof RemoveParticipant) {
			return (b instanceof RemoveParticipant)
			&& (RemoveParticipant(a)).getParticipantId().equals(
				(RemoveParticipant(b)).getParticipantId());
		} else if (a instanceof NoOp) {
			return b instanceof NoOp;
		} else if (a instanceof WaveletDocumentOperation) {
			if (!(b instanceof WaveletDocumentOperation)) {
				return false;
			}
			var a1:WaveletDocumentOperation= WaveletDocumentOperation(a);
			var b1:WaveletDocumentOperation= WaveletDocumentOperation(b);
			return (a1.getDocumentId() == (b1.getDocumentId())
				&& equal(a1.getOperation(), b1.getOperation()));
		} else {
			throw new Error("Unexpected WaveletOperation subtype: " + a);
		}
	}
	
	public function equalNullable(a:DocumentOperation, b:DocumentOperation):Boolean {return null;};
	public function equal(a:DocumentOperation, b:DocumentOperation):Boolean  {return null;};
	
}
}